package com.prp.gwt.client.widgets.admin;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SuggestBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.prp.gwt.client.admin.AdminListener;
import com.prp.gwt.client.vo.UserList;

public class AdminListPanel extends VerticalPanel {

	private FlexTable table = new FlexTable();
	private SuggestBox email;
	private HTML message = new HTML("");
	private static AdminListPanel adminList;
	private static AdminListPanel rootList;
	private UserList userList;
	

	public static AdminListPanel getAdminList() {
		if (adminList == null)
			adminList = new AdminListPanel();
		return adminList;
	}

	public static AdminListPanel getRootList() {
		if (rootList == null)
			rootList = new AdminListPanel();
		return rootList;
	}

	private AdminListPanel() {
		Button addButton = new Button("添加");
		Button refreshButton = new Button("刷新");
		email = new SuggestBox(AdminView.getInstance().getEmailOracle());
		email.setLimit(10);
		email.addKeyPressHandler(new KeyPressHandler() {

			@Override
			public void onKeyPress(KeyPressEvent event) {
				AdminListener.getInstance().getEmailSuggest(email.getText());
				email.showSuggestionList();
			}

		});

		addButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (AdminListPanel.this == AdminListPanel.getAdminList())
					AdminListener.getInstance().addAdmin(email.getText());
				else if (AdminListPanel.this == AdminListPanel.getRootList())
					AdminListener.getInstance().addRoot(email.getText());
			}

		});
		refreshButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if (AdminListPanel.this == AdminListPanel.getAdminList())
					AdminListener.getInstance().updateAdminList();
				else if (AdminListPanel.this == AdminListPanel.getRootList())
					AdminListener.getInstance().updateRootList();
				showMessage("正在刷新");
			}
		});
		HorizontalPanel panel = new HorizontalPanel();
		panel.add(new HTML("邮箱："));
		panel.add(email);
		panel.add(addButton);
		panel.add(refreshButton);
		panel.add(message);
		panel.setSpacing(10);
		table.setHTML(0, 0, "邮箱");
		table.setHTML(0, 1, "用户名");
		table.setCellSpacing(10);
		
		add(panel);
		add(table);
	}

	public Button getRemoveButton(final int n) {
		Button button = new Button("移除");
		button.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				if (AdminListPanel.this == AdminListPanel.getAdminList())
					AdminListener.getInstance().removeAdmin(
							userList.get(n).getEmail());
				else if (AdminListPanel.this == AdminListPanel.getRootList())
					AdminListener.getInstance().removeRoot(
							userList.get(n).getEmail());
			}

		});
		return button;
	}

	public void load(UserList userList) {
		this.userList = userList;
		int row = table.getRowCount();
		for (int i = row - 1; i > 0; i--)
			table.removeRow(i);
		for (int i = 0; i < userList.size(); i++) {
			int j = i + 1;
			table.setWidget(j, 0, new Label(userList.get(i).getEmail()));
			table.setWidget(j, 1, new Label(userList.get(i).getUsername()));
			table.setWidget(j, 2, getRemoveButton(i));
		}
		showMessage("");
	}

	public void showMessage(String text) {
		message.setText(text);
	}

}
